package Coder;

import java.util.Scanner;

public class Coder {
	public static void main(String args[]){
		
		Scanner input = new Scanner(System.in);
		
		String choice;
		System.out.println("Do you want to encode or decode?(type 'encode' or 'decode')");
		choice = input.nextLine();
		if(choice.equals("encode")){
		
                    System.out.println("Type the line you wish to encode.");
                    String toEncode;
                    toEncode= input.nextLine();
                    System.out.println("Type the keyword wich you will use to decode the encoded line, and on wich de encoding is based.");
                    String keyword;
                    keyword = input.nextLine();

                    char charsToEncode[] = new char[toEncode.length()];
                    charsToEncode = toEncode.toCharArray();
                    int intsToEncode[] = new int[toEncode.length()];

                    char charsKeyword[] = new char[keyword.length()];
                    charsKeyword = keyword.toCharArray();
                    int intsKeyword[] = new int[keyword.length()];

                    char charsEncoded[] = new char[toEncode.length()];

                    for(int i =0; i < toEncode.length(); i++){
                            int e = i;
                            while(e>=keyword.length()){
                                e = e-keyword.length();
                            }
                            intsToEncode[i]=charsToEncode[i];
                            intsKeyword[e]=charsKeyword[e];

                            int intEncoded = intsToEncode[i]+intsKeyword[e];
                            charsEncoded[i]=(char) intEncoded;
                    }

                    System.out.println("Encoded line: ");
                    for(char asd : charsEncoded){
                            System.out.print(asd);
                    }
		}else if(choice.equals("decode")){
                    System.out.println("Type the line you wish to decode.");
                    String toDecode;
                    toDecode= input.nextLine();
                    System.out.println("Type the keyword wich you will use to decode.");
                    String keyword;
                    keyword = input.nextLine();
                    
                    char charsToDecode[] = new char[toDecode.length()];
                    charsToDecode = toDecode.toCharArray();
                    int intsToDecode[] = new int[toDecode.length()];
			
                    char charsKeyword[] = new char[keyword.length()];
                    charsKeyword = keyword.toCharArray();
                    int intsKeyword[] = new int[keyword.length()];
			
                    char charsDecoded[] = new char[toDecode.length()];
			
                    for(int i =0; i < toDecode.length(); i++){
                            int e = i;
                            while(e>=keyword.length()){
                                e = e-keyword.length();
                            }
                            intsToDecode[i]=charsToDecode[i];
                            intsKeyword[e]=charsKeyword[e];

                            int intDecoded = intsToDecode[i]-intsKeyword[e];
                            charsDecoded[i]=(char) intDecoded;
                    }
                System.out.println("Decoded line: ");
                    for(char asd : charsDecoded){
                            System.out.print(asd);
                    }
                    
		}
		else{
			System.out.println("your argument is invalid");
		}
	}
}